package com.hzlj.position.locate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.dict.annotation.DictConvert;
import com.fz.common.redis.annotation.SyncLock;
import com.hzlj.position.config.config.PositionConfig;
import com.hzlj.position.locate.common.common.ReadUpdateDTO;
import com.hzlj.position.locate.common.dto.reportAudio.*;
import com.hzlj.position.locate.common.model.ReportAudio;
import com.hzlj.position.locate.converter.ReportAudioConverter;
import com.hzlj.position.locate.dao.ReportAudioDao;
import com.hzlj.position.locate.rest.AudioRest;
import com.hzlj.position.locate.service.ReportAudioService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 主动音频上报(ReportAudio)表服务接口
 *
 * @author makejava
 * @date 2024-01-15 10:49:16
 */
@Slf4j
@Service
public class ReportAudioServiceImpl implements ReportAudioService {
    @Resource
    private ReportAudioDao reportAudioDao;
    @Resource
    private PositionConfig positionConfig;
    @Resource
    private AudioRest audioRest;

    /**
     * 新增数据
     */
    @Override
    @SyncLock(key = "#locate.jzId")
    @Transactional(rollbackFor = Exception.class)
    public void report(ReportAudioDTO locate) {
        ReportAudio report = ReportAudioConverter.from(locate);
        //转文本
        this.fillAudioText(report);
        //保存
        this.reportAudioDao.save(report);
    }

    @Override
    public void report(List<ReportAudioPhoneAddDTO> dtos) {
        if (ObjectUtil.isEmpty(dtos)) {
            return;
        }
        List<ReportAudio> reportAudios = dtos.stream()
                .map(ReportAudioConverter::from)
                .collect(Collectors.toList());
        //转文本
        for (ReportAudio report : reportAudios) {
            this.fillAudioText(report);
        }
        //保存
        this.reportAudioDao.saveBatch(reportAudios);
    }


    @Override
    public void read(ReadUpdateDTO read) {
        ReportAudio report = BeanUtil.copyProperties(read, ReportAudio.class);
        this.reportAudioDao.updateReportAudio(report);
    }

    @Override
    @Transactional
    public void handle(ReportAudioHandleDTO dto) {
        this.reportAudioDao.handle(dto);
    }

    @Override
    public ReportAudioHandleQueryResultDTO getHandleResult(String id, Date belongDay) {
        return this.reportAudioDao.getHandleResult(id, belongDay);
    }

    /**
     * 查询列表：不分页
     */
    @Override
    @DictConvert
    public List<ReportAudioWithJzJbxxDTO> listReportAudio(ReportAudioQueryParamDTO dto) {
        return this.reportAudioDao.listReportAudio(dto);
    }

    /**
     * 查询列表：分页
     */
    @Override
    @DictConvert
    public QueryPageResultDTO<ReportAudioWithJzJbxxDTO> pageReportAudio(ReportAudioQueryParamDTO dto, PageParamDTO page) {
        return this.reportAudioDao.pageReportAudio(dto, page);
    }

    private void fillAudioText(ReportAudio report) {
        if (positionConfig.getAudio().getAudioText()) {
            AudioRest.ConvertResult convert = audioRest.convert(report.getId(), report.getJzId(), report.getFileIds());
            BeanUtil.copyProperties(convert, report, CopyOptions.create().ignoreNullValue());
        }
    }

}
